import os
import sys
import json
from function_utils import Human, Global


def list_dir(dir_path):
    def loop(items, tp):
        for _item in items:
            _path = os.path.join(root, _item)
            _type = 'LINK-%s'%tp if os.path.islink(_path) else tp
            st = os.stat(_path)
            result.append((_item, Human.size(st.st_size), Human.time(st.st_mtime), Human.mode(st.st_mode), Human.user(st.st_uid), _type))
    
    result = [dir_path]
    for root, dirs, files in os.walk(dir_path):
        loop(dirs, 'DIR')
        loop(files, 'FILE')
        return result


if __name__ == '__main__':
    real_path = os.path.realpath(sys.argv[1])
    print('{0}{1}{0}'.format(Global.split_flag, json.dumps(list_dir(real_path))))
